//
//  YGKJCustomerServiceVC.m
//  新项目
//
//  Created by 刘耀宗 on 2016/10/21.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "YGKJCustomerServiceVC.h"
#import "JGCollectionViewCell.h"
#import "JGHotHeaderView.h"
#import "JGCollectionReusableView.h"  //头部
#import "WTImageScroll.h"
#import "YGKJActivityVC.h"


#define  kColumns 2
#define kDeviceHight [UIScreen mainScreen].bounds.size.height
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width
#define JGMargin 10

@interface YGKJCustomerServiceVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *DataArrM;
@end

@implementation YGKJCustomerServiceVC
static NSString * const JGCollectionCellId = @"JGCollectionCellId";
static NSString *const JGSetHeaderCellId = @"JGSetHeaderCellId";
static NSString *const JGSetSVCellId = @"JGSetSVCellId";
static NSString *const JGSetHotHeaderCellId = @"JGSetHotHeaderCellId";


- (NSMutableArray *)DataArrM {
    if (!_DataArrM) {
        _DataArrM = [NSMutableArray array];
        [_DataArrM addObject:@"1"];
        [_DataArrM addObject:@"1"];
        [_DataArrM addObject:@"1"];
        [_DataArrM addObject:@"1"];
        [_DataArrM addObject:@"1"];
        [_DataArrM addObject:@"1"];
        
    }
    return _DataArrM;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"首页";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // Do any additional setup after loading the view, typically from a nib.
    
    //self.automaticallyAdjustsScrollViewInsets = YES;
    
    
    //创建collectionView
    [self createCollectionView];
    
}




//创建collectionView
- (void)createCollectionView {
    
    //首先需要创建一个布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //创建cllectionView对象
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView = collectionView;
    //collectionView.contentInset = UIEdgeInsetsMake(64, 0, 59, 0);
    collectionView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    
    [self.view addSubview:collectionView];
    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JGCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:JGCollectionCellId];
    //    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:JGCollectionCellId];
    
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JGHotHeaderView class]) bundle:nil] forCellWithReuseIdentifier:JGSetHotHeaderCellId];
    
    //    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:JGSetHeaderCellId];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JGCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:JGSetHeaderCellId];
    
}


#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

//指定有多少个子视图
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else{
        return 5;
    }
}

//指定子视图
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    JGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JGCollectionCellId forIndexPath:indexPath];
    //返回
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
//            cell.titleLabel.text = @"双色球";
//            cell.detailLabel.text = @"奖池8.6亿";
//            [cell.imageView setImage:[UIImage imageNamed:@"icon_scq"]];
//            cell.titleLabel.textColor = [UIColor blueColor];
            cell.imageView.image = [UIImage imageNamed:@"彩票_03"];
        }else if (indexPath.row == 1){
//            cell.titleLabel.text = @"重庆时时彩";
//            cell.detailLabel.text = @"免费分钱,速来拿";
//            [cell.imageView setImage:[UIImage imageNamed:@"icon_cqssc"]];
//            cell.titleLabel.textColor = [UIColor redColor];
             cell.imageView.image = [UIImage imageNamed:@"彩票_05"];
        }else if (indexPath.row == 2){
      cell.imageView.image = [UIImage imageNamed:@"彩票_09"];
        }else if (indexPath.row == 3){
         cell.imageView.image = [UIImage imageNamed:@"彩票_10"];
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
              cell.imageView.image = [UIImage imageNamed:@"彩票_13"];
        }else if (indexPath.row == 1){
           cell.imageView.image = [UIImage imageNamed:@"彩票_14"];
        }else if (indexPath.row == 2){
             cell.imageView.image = [UIImage imageNamed:@"彩票_17"];
            
        }else if (indexPath.row == 3){
             cell.imageView.image = [UIImage imageNamed:@"彩票_18"];
            
        }else if (indexPath.row == 4){
              cell.imageView.image = [UIImage imageNamed:@"彩票_21"];
            
        }else if(indexPath.row == 5){
                cell.imageView.image = [UIImage imageNamed:@"彩票_10"];
            
        }else if (indexPath.row == 6){
               cell.imageView.image = [UIImage imageNamed:@"彩票_10"];
            
        }
    }
    return cell;
}

// 显示表头的数据
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader){
        // JGHeaderView
        JGCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:JGSetHeaderCellId forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            JGCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:JGSetHeaderCellId forIndexPath:indexPath];
            header.SVHeight.constant = 25;
            
            
            UIView *imageScorll = [WTImageScroll ShowLocationImageScrollWithFream:CGRectMake(0, 0, kDeviceWidth, 110) andImageArray:@[@"banner1",@"banner2",@"banner3"] andBtnClick:^(NSInteger tagValue) {
                //NSLog(@"点击banner图---%@"),@(tagValue));
            }];
            [header.headerV setBackgroundColor:[UIColor whiteColor]];
            
            //  [header.SV addSubview:imageScorll];
            [header.headerV addSubview:imageScorll];
        }else {
            
            JGCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:JGSetHeaderCellId forIndexPath:indexPath];
            header.HeaderHeight.constant = 25;
            [header.SV setHidden:YES];
            //[header.headerV setHidden:YES];
            header.SVHeight.constant = 25;
            [header.headerV setBackgroundColor:[UIColor whiteColor]];
        }
        
        
        //   header.backgroundColor = [UIColor redColor];
        
        return header;
        
    }
    else{
        return nil;
    }
    // header.backgroundColor = [UIColor redColor];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld - %ld",indexPath.section, indexPath.row);
    whenPush(YGKJActivityVC)
}

// 表头尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(kDeviceWidth, 135);
    }
    return CGSizeMake(kDeviceWidth, 25);
}




- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}



//返回每个子视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        // CGFloat itemWidth = (kDeviceWidth - (JGMargin * 1.5) * (kColumns + 1) ) / kColumns ;
        CGFloat itemWidth = kDeviceWidth / kColumns  ;
        return CGSizeMake(itemWidth, 110);
    }else {
        
        // CGFloat itemWidth = (kDeviceWidth - (JGMargin * 1.5) * (kColumns + 2) ) / (kColumns + 1) ;
        CGFloat itemWidth = kDeviceWidth / kColumns ;
        return CGSizeMake(itemWidth, 110);
    }
    
    
}

//设置每个子视图的缩进
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
    //return UIEdgeInsetsMake(0, (JGMargin * 1.5), 0, (JGMargin * 1.5));
}


//设置子视图上下之间的距离
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    // return (JGMargin * 1.5);
    return 0;
    
}

////设置子视图左右之间的距离
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return kPadding;
//}


@end
